package org.zj.springstyle.mvc.util;

import org.zj.springstyle.mvc.domain.SuperDomain;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;

/**
 * Created by zhaoji on 14/12/23.
 */
public class Reflect4domain {

    /**
     * 获取前台传递过来的类名
     * @param request
     * @return
     */
    public static SuperDomain gainClass(String formPath,HttpServletRequest request){
        SuperDomain o = null;
        try {
            try {
                o = (SuperDomain)Class.forName(formPath).newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            Field []fields = o.getClass().getDeclaredFields();
            for(Field f : fields){
                f.setAccessible(true);
                try {
                    f.set(o , request.getParameter(f.getName()));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                f.setAccessible(false);
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return o;
    }

}
